import java.io.File;

import menu.Menu;
import menu.MenuElement;

import display.MenuDisplay;
import display.TextMenuDisplay;

import reader.MenuReader;
import reader.XMLMenuReader;

//-----------------------------------------------------------------
// This is a menu tool which facilitates the creation of a menu.
//
// Menus are stored as xml files in the following format:
//   <menu>
//      <prompt> The user prompt for this menu. </prompt>
//      <title> The title for this menu. </title>
//      <element id="X"> A list of the elements which compose this menu.
//                       It expects a numerical identifier which is
//                       returned to the calling application.
//         <hotkey> The hotkey which will activate this element. </hotkey>
//         <description> The description of this element. </description>
//         <link> An optional parameter which contains a relative link
//                to the file.
//         </link>
//      </element>
//   </menu>
//-----------------------------------------------------------------
public class TextMenuApplication implements MenuApplication {
    private MenuDisplay aDisplay;
    private MenuReader aReader;
    private Menu aMenu;
    
    public TextMenuApplication() {
        aReader = new XMLMenuReader();
        aMenu = aReader.getMenu(new File("default.menu"));
        aDisplay = new TextMenuDisplay(aReader);
    }
    
    public TextMenuApplication(String pMenuLocation) {
        aReader = new XMLMenuReader();
        aMenu = aReader.getMenu(new File(pMenuLocation));
        aDisplay = new TextMenuDisplay(aReader);
    }
    
    public void displayMenu() {
        aMenu = aDisplay.display(aMenu);
    }

    public int getSelection() {
        return aDisplay.getSelection();
    }

    public void setItems(File pMenuFile) {
        aMenu = aReader.getMenu(pMenuFile);
    }
    
    // Sample application code
    // For testing purposes
    public static void main(String[] args) {
        final String lMenuFile = "default.menu";
        TextMenuApplication lMenu = new TextMenuApplication(lMenuFile);
        
        int lChoice;
        do {
            lMenu.displayMenu();
            lChoice = lMenu.getSelection();
            System.out.println(
                    ">>> Menu element " + lChoice + " was selected.");
        } while(lChoice != 1);
        
        System.out.println("Quitting. Good bye!");
    }
}
